package com.example.springboot.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.utils.TokenUtils;
import org.springframework.web.bind.annotation.*;
import com.example.springboot.common.Result;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.example.springboot.service.ICCollectionService;
import com.example.springboot.entity.CCollection;

import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/c-collection")
    public class CCollectionController {
    
    @Resource
    private ICCollectionService cCollectionService;

    //查找所有
    @GetMapping
    public Result findAll() {
        return Result.success(cCollectionService.list());
    }

    //根据用户查找收藏列表
    @GetMapping("/getByUser")
    public Result getByUser(@RequestParam Integer userId) {
        return Result.success(cCollectionService.getByUser(userId));
    }

    //查询当前用户的收藏列表
    @GetMapping("/getMine")
    public Result getMine() {
        return Result.success(cCollectionService.getByUser(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId()));
    }

    //查找是否有收藏关系
    @GetMapping("/judge")
    public Result judge(@RequestParam Integer creativeId) {
        return Result.success(cCollectionService.judge(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId(),creativeId));
    }

    //收藏
    @PostMapping
    public Result insertCollection(@RequestBody CCollection cCollection) {
        cCollection.setUserId(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId());
        return cCollectionService.insertCollection(cCollection);
    }

    //取消收藏
    @DeleteMapping
    public Result delete(@RequestParam Integer creativeId) {
        CCollection cCollection = new CCollection();
        cCollection.setUserId(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId());
        cCollection.setCreativeId(creativeId);
        return cCollectionService.removeCollection(cCollection);
    }

}

